package com.study.base

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.NonNull
import androidx.annotation.Nullable
import androidx.fragment.app.Fragment

/**
 * <pre>
 *     author : fauks
 *     time   : 2020/03/23
 *     desc   :
 * </pre>
 */
abstract class BaseFragment : Fragment() {

    lateinit var activity: Activity
    var rootView: View? = null

    override fun onAttach(context: Context) {
        super.onAttach(context)
        activity = context as Activity
    }

    @Nullable
    override fun onCreateView(@NonNull inflater: LayoutInflater, @Nullable container: ViewGroup?, @Nullable savedInstanceState: Bundle?): View? {
        val layoutId = getLayoutId()
        if (layoutId > 0) {
            rootView = inflater.inflate(layoutId, container, false)
        } else {
            rootView = generateContentView()
        }

        return rootView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
        initData()
    }

    fun generateContentView(): View? {
        return null
    }

    abstract fun getLayoutId(): Int

    open fun initView() {}

    open fun initData() {}
}